package com.eascs.crm.nsEditDetail.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.entity.O2oMainStoreInfoEntity;
import com.eascs.crm.nsEditDetail.service.CustomerShopInfoService;
import com.eascs.crm.nsEditDetail.service.IMainStoreInfoService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import static com.eascs.site.view.HtmlRestResult.popHtmlFragment;

import java.util.Date;


/**
 * @Description:
 *               <p>
 *               内审信息编辑--仓库信息
 *               </p>
 * @author LMX
 */
@Controller
@RequestMapping("/mainStoreInfoRest")
public class MainStoreInfoRestController extends BaseRestController {
	private static Logger logger = LoggerFactory.getLogger(MainStoreInfoRestController.class);

	@Reference
	IMainStoreInfoService mainStoreInfoService;

	@Autowired
	CustomerShopInfoService shopInfoService;
	@Autowired
	O2oLoanapplyService  o2oLoanapplyService;
	@Value("${web.crm.url}")
	private String crmUrl;
	
	/**
	 * @Description:
	 *               <p>
	 *                仓库信息详情
	 *               </p>
	 * @author LMX
	 */
	@RequestMapping(value = "/detailPage", method = RequestMethod.GET)
	public HtmlRestResult queryIndex(String storeId, String type, String approvalStatus) {
		logger.info("经营主体查询loanapplyNo：【{}】", storeId);
		O2oMainStoreInfoEntity info = mainStoreInfoService.getEntityById(storeId);
		ViewContext.put("view", info);
		ViewContext.put("type", type);
		ViewContext.put("approvalStatus", approvalStatus);
		return popHtmlFragment("/nsEditDetail/main_store_info_view");
	}
	
	/**
	 * @Description:
	 *               <p>
	 *               新增
	 *               </p>
	 * @author LMX
	 */
	@RequestMapping(value = "/addPage")
	public HtmlRestResult addIndex(String loanapplyNo) {
		logger.info("仓库信息查询loanapplyNo：【{}】", loanapplyNo);
		ViewContext.put("loanapplyNo", loanapplyNo);
		ViewContext.put("crmUrl", crmUrl);// 地址选择URL
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/main_store_info_add");
	}
	
	/**
	 * 
	 * @Description:
	 *               <p>
	 *               保存 新增
	 *               </p>
	 * @author LMX
	 */
	@RequestMapping("/saveAdd")
	public BaseRestResult<String> assetsSaveAdd(O2oMainStoreInfoEntity form) {
		createO2oMainShopInfo(form);
		form.setIsDel(StatusContexts.O2O_NO);// 不删除（只存数据库不显示）
		boolean flag = mainStoreInfoService.insert(form);// 新增
		if (flag) {
			return BaseRestResult.info("添加仓库信息成功").andRefresh();
		} else {
			return BaseRestResult.warn("添加仓库信息失败").andRefresh();
		}
	}
	
	/**
	 * @Description:
	 *               <p>
	 *               修改
	 *               </p>
	 * @author LMX
	 */
	@RequestMapping(value = "/updatePage")
	public HtmlRestResult updateIndex(String storeId) {
		logger.info("仓库信息查询loanapplyNo：【{}】", storeId);
		O2oMainStoreInfoEntity entity = mainStoreInfoService.getEntityById(storeId);
		ViewContext.put("view", entity);
		ViewContext.put("loanapplyNo", entity.getLoanapplyNo());
		ViewContext.put("crmUrl", crmUrl);// 地址选择URL
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/main_store_info_update");
	}

	/**
	 * 
	 * @Description:
	 *               <p>
	 *               保存 新增/修改
	 *               </p>
	 * @author LMX
	 */
	@RequestMapping("/save")
	public BaseRestResult<String> assetsSave(O2oMainStoreInfoEntity form) {
		O2oMainStoreInfoEntity entity = mainStoreInfoService.getEntityById(form.getId());
		if (entity == null) {
			createO2oMainShopInfo(form);
			form.setIsDel(StatusContexts.O2O_NO);// 不删除（只存数据库不显示）
			boolean flag = mainStoreInfoService.insert(form);// 新增
			if (flag) {
				return BaseRestResult.info("添加仓库信息成功").andRefresh();
			} else {
				return BaseRestResult.warn("添加仓库信息失败").andRefresh();
			}
		} else {
			if (StringUtil.isBlank(form.getId())) {
				return BaseRestResult.warn("更新仓库信息失败:记录不存在").andRefresh();
			}
			O2oMainStoreInfoEntity info = mainStoreInfoService.getEntityById(form.getId());
			form = modifyO2oMainShopInfo(info, form);
			form.setIsDel(StatusContexts.O2O_NO);// 不删除（只存数据库不显示）
			boolean flag = mainStoreInfoService.update(form);// 修改
			if (flag) {
				return BaseRestResult.info("修改仓库信息成功").andRefresh();
			} else {
				return BaseRestResult.warn("修改仓库信息失败").andRefresh();
			}
		}
	}
	
	/**
	 * @Description:
	 *               <p>
	 *               删除
	 *               </p>
	 * @author LMX
	 */
	@RequestMapping("/del")
	public BaseRestResult<String> mainStoreInfoEntityDel(String storeId) {
		O2oMainStoreInfoEntity entity = mainStoreInfoService.getEntityById(storeId);
		modifyO2oMainShopInfo(entity);
		boolean flag = mainStoreInfoService.delete(storeId);
		if(flag) {
			return BaseRestResult.info("删除仓库信息成功").andRefresh();
		} else {
			return BaseRestResult.warn("删除仓库信息失败").andRefresh();
		}
	}
	
	/**
	 * @Description:
	 *               <p>
	 *               脱敏
	 *               </p>
	 * @author LMX
	 */
	@RequestMapping("/view_address")
	public BaseRestResult<String> view_address(String storeId) {
		O2oMainStoreInfoEntity info = mainStoreInfoService.getEntityById(storeId);
		ViewContext.put("view", info);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/view_address_store");
	}


	private void createO2oMainShopInfo(O2oMainStoreInfoEntity info){
		User user = UserHolder.getUser();
		info.setCreateDate(new Date());
		info.setModifyDate(new Date());
		info.setCreateId(user.getNo());
		info.setModifyId(user.getNo());
		info.setCreator(user.getCname());
		info.setModifier(user.getCname());
	}

	private void modifyO2oMainShopInfo(O2oMainStoreInfoEntity info){
		User user = UserHolder.getUser();
		info.setModifyDate(new Date());
		info.setModifyId(user.getNo());
		info.setModifier(user.getCname());
	}
	
	private O2oMainStoreInfoEntity modifyO2oMainShopInfo(O2oMainStoreInfoEntity info,O2oMainStoreInfoEntity form){
		User user = UserHolder.getUser();
		form.setModifyDate(new Date());
		form.setModifyId(user.getNo());
		form.setModifier(user.getCname());
		
		form.setCreateId(info.getCreateId());
		form.setCreateDate(info.getCreateDate());
		form.setCreator(info.getCreator());
		return form;
	}
}
